/**
 * @file useFlatSpec
 * @description useFlatSpec
 * @author fanxueliang<fanxueliang
 */

import {reactive} from 'vue';
import {UnionSpecItem} from '@jsw/report-core';
import {keyBy, map} from 'lodash-es';
import {DatasourceType} from '../interface';

import {getSpecAttr} from '../utils/getSpecAttr';
import {getSpecRules} from '../utils/getSpecRules';

import {getFlatSpec} from '../utils/getFlatSpec';

// interface PropsType {
//     spec: UnionSpecItem[];
//     regexRules: DatasourceType[];
//     modifySpec: any[];
//     actionLinkList: DatasourceType[];
// }

export const useFlatSpecAttr = (
    spec: UnionSpecItem[],
    regexRules: DatasourceType[],
    modifySpec: any[],
    actionLinkList: DatasourceType[]
) => {
    // const {spec, regexRules, modifySpec, actionLinkList} = props;
    const modifySpecMap = keyBy(modifySpec, 'path');
    const flatSpec = getFlatSpec(spec, modifySpecMap, '');
    return reactive(
        map(flatSpec, (item) => {
            return getSpecRules(getSpecAttr(item, actionLinkList), {
                regexRules
            });
        })
    );
};
